๐ณ Docker Foundations
Let's start from "What is Docker?", assuming you've never seen Docker before.
The goal of this lesson is not to memorize commands. By the end, you should understand:
- What Docker is
- Why it exists
- What a container is
- What an image is
- Container vs VM
- Docker Engine
- Docker Hub
- Dockerfile
- Registry
- Basic Docker architecture
- Your first Docker commands
1. ๐ง Imagine You Built an App
Suppose you write a small Python application.
It works perfectly on your computer.
You send it to your friend.
Your friend runs it and gets:
ERROR
Python version mismatch
You fix that.
Then another person tries it:
ERROR
Missing library
Then you deploy it to a server:
ERROR
Configuration is different
๐ญ
The problem is:
Your application depends on its environment.
Your application might need:
Python 3.12
Flask 3.x
requests
specific system libraries
environment variables
configuration
Your computer has all of these.
Another computer might not.
2. ๐ฆ Docker's Big Idea
Docker basically says:
"Let's package the application together with everything it needs so it can run consistently."
Think about moving houses.
Instead of saying:
"Here is my furniture. Good luck figuring out how to arrange everything."
You put everything into a standardized container:
๐ฆ Application
๐ฆ Libraries
๐ฆ Dependencies
๐ฆ Configuration
Then you can move that container between environments.
Your laptop
โ
Docker
โ
๐ฆ Container
โ
Cloud server
That's the basic idea.
3. ๐ณ So What Exactly Is Docker?
Docker is a platform for building, packaging, distributing, and running applications in containers.
The important word is:
Container
A container is an isolated environment where an application runs.
For example:
๐ฆ Container
โ
โโโ Python
โโโ Flask
โโโ Application code
โโโ Dependencies
โโโ Configuration
Docker runs that container using the host operating system's kernel.
4. ๐ Container = Small Isolated Environment
Imagine an apartment building.
The building is the computer.
Each apartment is a container.
๐ข Linux Host
โ
โโโ ๐ Container A
โ โโโ Python application
โ
โโโ ๐ Container B
โ โโโ Node.js application
โ
โโโ ๐ Container C
โโโ Nginx
Each container is isolated from the others.
They can have different:
- Applications
- Libraries
- Dependencies
- Configuration
Yet they can all run on the same host.
5. ๐ค Why Not Just Use Virtual Machines?
Excellent question.
Before containers became extremely popular, we commonly used virtual machines (VMs).
A VM looks roughly like:
Physical Computer
โ
โโโ Host OS
โ
โโโ VM 1
โ โโโ Guest OS
โ โโโ Application
โ
โโโ VM 2
โ โโโ Guest OS
โ โโโ Application
โ
โโโ VM 3
โโโ Guest OS
โโโ Application
Each VM has its own operating system.
That can be relatively heavy.
Containers work differently:
Physical Computer
โ
โโโ Host OS
โ
โโโ Docker
โ
โโโ Container 1
โ โโโ Application
โ
โโโ Container 2
โ โโโ Application
โ
โโโ Container 3
โโโ Application
Containers share the host's kernel.
So they are generally:
- Faster to start
- More lightweight
- Easier to package
- Easier to deploy at scale
6. ๐ง Container vs Virtual Machine
Here's the mental model you need:
| Virtual Machine | Container |
|---|---|
| Contains a guest OS | Shares host kernel |
| Usually heavier | Usually lighter |
| Slower startup | Usually very fast startup |
| More resource overhead | Less resource overhead |
| Strong OS-level isolation | Process-level isolation |
| VM image | Container image |
Don't interpret this as:
"Containers are always better than VMs."
They're different technologies with different strengths.
And in real cloud environments, containers frequently run inside VMs.
For example:
Cloud
โ
โโโ Virtual Machine
โ
โโโ Docker
โ
โโโ Container
โโโ Container
โโโ Container
7. ๐งฉ The Most Important Docker Concepts
You need to understand these six words:
Image
Container
Dockerfile
Docker Engine
Registry
Docker Compose
We'll take them one by one.
8. ๐ธ What Is a Docker Image?
An image is a packaged, immutable template used to create containers.
Think:
๐ง Image = recipe/template
๐ฐ Container = actual running thing created from that template
For example:
Docker Image
โ
run
โ
Container
You might have an image called:
nginx
You can create multiple containers from it:
nginx image
โ
โโโ Container 1
โโโ Container 2
โโโ Container 3
The image itself isn't the running application.
It's the blueprint.
9. ๐ฆ What Is a Container?
A container is a running instance of an image.
Think:
Image
โ
"Make me a container"
โ
Container
For example:
nginx image
โ
nginx container
You can have:
Image: nginx
Containers:
โโโ website-1
โโโ website-2
โโโ website-3
10. ๐ What Is a Dockerfile?
Now we get to something extremely important.
Suppose you want to create your own Docker image.
You need to tell Docker:
"Start with this base environment, copy my application, install these dependencies, and run this command."
You write those instructions in a:
Dockerfile
For example:
FROM python:3.12
WORKDIR /app
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "app.py"]
Don't worry about understanding every line yet.
We'll study Dockerfiles deeply later.
For now:
Dockerfile
โ
docker build
โ
Docker Image
โ
docker run
โ
Container
This relationship is fundamental.
11. ๐ญ Docker's Factory Analogy
Think about a factory.
Dockerfileโ
๐ Instructions for making the product.
Imageโ
๐ฆ Finished packaged product.
Containerโ
๐ Product currently being used.
So:
Dockerfile
โ
BUILD
โ
Image
โ
RUN
โ
Container
Memorize this.
12. ๐ What Is a Docker Registry?
Suppose you've created an image on your laptop.
Now you want your cloud server to use it.
How do you get it there?
You can upload it to a container registry.
A registry is basically a place where container images are stored and distributed.
One popular registry is:
Docker Hub
Other examples include cloud-provider registries such as:
- Amazon ECR
- Azure Container Registry
- Google Artifact Registry
Conceptually:
Your computer
โ
โ docker push
โ
Registry
โ
โ docker pull
โ
Cloud server
13. ๐ณ What Is Docker Hub?
Docker Hub is a public registry containing Docker images.
You can find images for things like:
nginx
redis
postgres
python
node
ubuntu
Instead of building everything from scratch, you can use an existing image as your starting point.
For example:
FROM python:3.12
means:
"Use the Python 3.12 image as my starting point."
14. โ๏ธ What Is Docker Engine?
Docker needs something to actually manage containers.
That's where the Docker Engine comes in.
Simplified:
You
โ
โ docker run
โ
Docker CLI
โ
โ
Docker Engine
โ
โโโ Images
โโโ Containers
โโโ Networks
โโโ Volumes
The Docker Engine is the underlying technology that creates and manages containers.
15. ๐ป Docker CLI
When you type:
docker run nginx
you're using the Docker command-line interface (CLI).
The CLI communicates with the Docker Engine.
Think:
You
โ
Docker CLI
โ
Docker Engine
โ
Container
16. ๐ Your First Docker Command
Once Docker is installed, try:
docker --version
You might get something like:
Docker version 27.x.x
That tells you Docker is installed.
Then:
docker info
This gives information about your Docker installation and engine.
17. ๐ See Your Docker Images
Run:
docker images
This shows images currently stored locally.
You might initially see:
REPOSITORY TAG IMAGE ID
Maybe nothing is there yet.
That's completely fine.
18. ๐ฆ Download an Image
Let's download an Nginx image:
docker pull nginx
Think:
"Docker, go to a registry and download the
nginximage."
Then:
docker images
You should now see something like:
REPOSITORY TAG IMAGE ID
nginx latest xxxxxxxxx
You've downloaded your first Docker image.
19. ๐ Create Your First Container
Now run:
docker run nginx
What's happening?
nginx image
โ
Docker creates
โ
nginx container
โ
nginx starts
If everything works, Nginx starts running inside the container.
But there's an interesting problem.
Your terminal may appear to be "stuck."
It's not actually stuck.
The container is running in the foreground.
Press:
Ctrl + C
to stop it.
20. ๐ Run It in the Background
Usually, servers need to keep running while you continue using your terminal.
Use:
docker run -d nginx
-d means:
detached mode
In simple terms:
"Run this container in the background."
Docker will return a container ID.
21. ๐ See Running Containers
Now:
docker ps
You'll see something similar to:
CONTAINER ID IMAGE COMMAND STATUS
abc123 nginx ... Up ...
This means:
"Show me currently running containers."
22. ๐ Stop a Container
Suppose the container ID is:
abc123
You can stop it:
docker stop abc123
Or you can use its name:
docker stop <container-name>
Then:
docker ps
It won't appear because it isn't running anymore.
23. ๐ Where Did My Container Go?
Run:
docker ps -a
The -a means:
Show all containers, including stopped ones.
You'll see your previous container.
This distinction is important:
docker ps
= running containers
docker ps -a
= all containers
24. ๐๏ธ Remove a Container
If you don't need the stopped container anymore:
docker rm <container-id>
Then:
docker ps -a
It should be gone.
25. ๐ง Important Docker Commands
For now, memorize only these:
docker --version
docker info
docker images
docker pull
docker run
docker ps
docker ps -a
docker stop
docker rm
Don't try to memorize 100 Docker commands.
Understand what each one means.
26. ๐ But How Do I Access Nginx?
This is where Docker networking begins.
If you run:
docker run -d nginx
Nginx is running inside the container.
That doesn't automatically mean you can access it from your browser through your computer's port 80.
We need port mapping.
Run:
docker run -d -p 8080:80 nginx
The important part is:
8080:80
It means:
Host port 8080
โ
Container port 80
Think:
Your computer
localhost:8080
โ
โ
Docker
โ
โ
Container
port 80
โ
โ
Nginx
Now open:
http://localhost:8080
in your browser.
You should see the Nginx welcome page.
๐ You just exposed a containerized web server to your computer.
27. ๐ง Port Mapping
This syntax:
-p HOST_PORT:CONTAINER_PORT
is incredibly important.
For example:
-p 8080:80
means:
Host Container
8080 โ 80
Another example:
-p 3000:3000
means:
Host Container
3000 โ 3000
28. ๐ See Container Details
You can inspect a container:
docker inspect <container-id>
This gives you a huge amount of information.
For example:
- Network configuration
- IP address
- Mounts
- Environment
- Image
- Ports
- Runtime configuration
We'll use this heavily later for troubleshooting.
29. ๐ View Container Logs
Suppose your application is running but something isn't working.
You can look at its logs:
docker logs <container-id>
This is extremely important.
Think:
"Container, tell me what happened."
For example:
docker logs my-container
We'll later use this for debugging real applications.
30. ๐ฅ๏ธ Execute a Command Inside a Container
You can enter a running container using:
docker exec -it <container-id> bash
Sometimes the container doesn't have bash, so you'll use:
docker exec -it <container-id> sh
Now you're effectively inside the container's environment.
For example:
ls
might show:
bin
etc
usr
var
...
You can then exit:
exit
This is extremely useful for troubleshooting.
๐ง The Docker Mental Model
This is what I want you to understand before we go any further:
Docker
โ
โโโโโโโโโโโโโโผโโโโโโโโโโโโโ
โ โ โ
Images Containers Networks
โ โ
โ โ
โ Running
โ applications
โ
โ
Built from
Dockerfile
โ
โ
Registry
And the most important pipeline:
Dockerfile
โ
โ docker build
โ
Image
โ
โ docker run
โ
Container
โ
โ docker push
โ
Registry
โ
โ docker pull
โ
Another machine
๐ฏ Your First Docker Lab
If Docker is installed, don't just read. Do this yourself.
Step 1โ
docker --version
Step 2โ
docker pull nginx
Step 3โ
docker images
Step 4โ
docker run -d --name my-nginx -p 8080:80 nginx
Notice that we gave our container a name:
my-nginx
Now:
docker ps
Step 5โ
Open:
http://localhost:8080
Step 6โ
Check its logs:
docker logs my-nginx
Step 7โ
Inspect it:
docker inspect my-nginx
Step 8โ
Enter the container:
docker exec -it my-nginx bash
Then:
ls
and:
cat /etc/os-release
Then:
exit
Step 9โ
Stop it:
docker stop my-nginx
Step 10โ
Check all containers:
docker ps -a
Step 11โ
Remove it:
docker rm my-nginx
๐งช Challenge โ Don't Look Up the Commands
After completing the lab, see if you can answer these without looking back:
1. What is Docker?โ
2. What is a container?โ
3. What is an image?โ
4. What's the difference between an image and a container?โ
5. What creates an image?โ
6. Where can Docker images be stored/shared?โ
7. What does this mean?โ
-p 8080:80
8. What's the difference between:โ
docker ps
and:
docker ps -a
9. What does this do?โ
docker logs my-nginx
10. What does this do?โ
docker exec -it my-nginx bash